var utils = require('../../../utils/util.js')
var c = require('../../../utils/config.js')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        region: [],
        address_name:"",
        address_phone:"",
        address:"",
        is_default:0,
        checked:false,
        id:0
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        utils.checkLogin();
        if (options.id){
            this.setData({
                id: options.id
                
            })
            this.getAddrInfo();
        }
    },
    getAddrInfo:function(){
        var that = this;
        var token = wx.getStorageSync("userinfo").wap_token;
        var data = {
            url: "/wsgoods/Useraddress/details",
            token: token,
            id: this.data.id
        };
        utils.request(data).then(function (res) {
            if (res.code == 0) {
                var data = res.data.addrinfo;
                that.setData({
                    address_name: data.address_name,
                    address_phone: data.address_phone,
                    address: data.address,
                    is_default: data.is_default,
                    region: [data.province, data.city, data.district],
                    checked: data.is_default==1?true:false
                })
            } else {
                wx.showToast({
                    title: res.msg,
                    icon: "none"
                })
            }
        });
    },
    changeDefault:function(){//更改默认
        var checked = this.data.checked?false:true;
        var is_default = checked?1:0;
        this.setData({
            is_default: is_default,
            checked: checked
        })
    },
    bindRegionChange: function (e) {
        this.setData({
            region: e.detail.value
        })
    },
    editaddress_name:function(e){
        this.setData({
            address_name: e.detail.value
        })
    },
    editaddress_phone:function(e){
        this.setData({
            address_phone: e.detail.value
        })
    },
    editAddress:function(e){
        this.setData({
            address: e.detail.value
        })
    },
    submit:function(){
        var address_name = this.data.address_name;
        var address_phone = this.data.address_phone;
        var address = this.data.address;
        var is_default = this.data.is_default;
        var region = this.data.region;
        if(address_name == ""){
            wx.showToast({
                title: '请输入收货人姓名',
                icon:"none"
            })
            return false;
        };
        if (address_phone == "") {
            wx.showToast({
                title: '请输入收货人电话',
                icon: "none"
            })
            return false;
        };
        var myreg = /^[1][0-9]{10}$/;
        if (address_phone == "" || !myreg.test(address_phone)) {
          wx.showToast({
            title: '手机号有误！',
            icon: 'none',
            duration: 1500,
          });
          return false;
        }
        if (address == "") {
            wx.showToast({
                title: '请输入收货地址',
                icon: "none"
            })
            return false;
        };
        if(region.length == 0){
            wx.showToast({
                title: '请选择地区',
                icon: "none"
            })
            return false;    
        }
        var that = this;
        var token = wx.getStorageSync("userinfo").wap_token;
        if(this.data.id == 0){
            var data = {
                url: "/wsgoods/Useraddress/add",
                addressname: address_name,
                token: token,
                province: region[0],
                city: region[1],
                district: region[2],
                addressmx: address,
                addresstel: address_phone,
                is_default: is_default
            };
        }else{
            var data = {
                url: "/wsgoods/Useraddress/edit",
                addressname: address_name,
                token: token,
                province: region[0],
                city: region[1],
                district: region[2],
                addressmx: address,
                addresstel: address_phone,
                is_default: is_default,
                addressid: this.data.id
            };
        }
        utils.request(data).then(function (res) {
            if (res.code == 0) {
                wx.showToast({
                    title: res.msg,
                    icon: "success"
                })
                setTimeout(function(){
                    wx.navigateBack({
                        delta: 1,
                    })
                },2000);
            } else {
                wx.showToast({
                    title: res.msg,
                    icon:"none"
                })
            }
        });
    }
})